# coding = UTF-8
# @Time : 2022/05/17 15:12
# @Author : PP_YY
# @File : 实验4.py
# @Description : 输入两个字符串，计算每个字符串中字符的TF,IDF以及TF.IDF的值
# 其中lg以10为底 IDF = lg(总串数/存在该元素的串数)

import tkinter
import numpy
import math


def get_items(get_list):
	tmp_list = []
	for i in range(len(get_list)):
		tmp_list += list(get_list[i])

	tmp_list = numpy.unique(tmp_list)
	return tmp_list


def calculate_TF(str_to_cal,list2):
	denominator = len(str_to_cal)
	ret_list = [0] * len(list2)
	for i in range(len(list2)):
		ret_list[i] = str_to_cal.count(list2[i])/denominator

	return ret_list

def calculate_IDF(to_one_list,all_list):
	ret_list = [0] * len(all_list)
	tmp_list = [0] * len(all_list)
	molecule = len(to_one_list)
	for i in range(len(all_list)):
		for j in range(molecule):
			if all_list[i] in to_one_list[j]:
				tmp_list[i] += 1
	# print(tmp_list)
	for i in range(len(all_list)):
		ret_list[i] = math.log10(molecule/tmp_list[i])

	return ret_list


def calculate_TF_IDF(TF,IDF):
	ret_list = [0] * len(TF)
	for i in range(len(TF)):
		ret_list[i] = TF[i] * IDF[i]
	return ret_list
	

def add_header(list1,all_list):
	ret_str = ""
	for i in range(len(list1)):
		ret_str = ret_str + all_list[i] + ":" + str(list1[i]) + '\n'
	return ret_str


def main():
	global entry_string1,entry_string2
	X = en1.get()
	Y =en2.get()
	to_one_list = [X,Y]
	all_list = get_items(to_one_list) #将单独的所有元素取出来

	X_toTF = calculate_TF(X,all_list)
	Y_toTF = calculate_TF(Y,all_list)

	IDF = calculate_IDF(to_one_list,all_list)

	X_TF_IDF = calculate_TF_IDF(X_toTF,IDF)
	Y_TF_IDF = calculate_TF_IDF(Y_toTF,IDF)

	print(X_toTF)
	print(Y_toTF)
	print(IDF)
	print(X_TF_IDF)
	print(Y_TF_IDF)

	en3.insert('1.0',add_header(X_toTF,all_list))
	en4.insert('1.0',add_header(Y_toTF,all_list))
	en5.insert('1.0',add_header(IDF,all_list))
	en6.insert('1.0',add_header(X_TF_IDF,all_list))
	en7.insert('1.0',add_header(Y_TF_IDF,all_list))


top = tkinter.Tk()
top.title('TF.IDF计算实验')
top.geometry('550x350')

lab1 = tkinter.Label(top, text = "s1")
lab1.place(x = 25, y = 10)
en1 = tkinter.Entry(top, width = 45)
en1.place(x = 60, y = 10)

lab2 = tkinter.Label(top, text = "s2")
lab2.place(x = 25, y = 45)
en2 = tkinter.Entry(top, width = 45)
en2.place(x = 60, y = 45)

lab3 = tkinter.Label(top, text = "s1 TF")
lab3.place(x = 25, y = 85)
en3 = tkinter.Text(top, bd=1, font=('等线', 10), width=20, height=5)
en3.place(x = 25, y = 110)

lab4 = tkinter.Label(top, text = "s2 TF")
lab4.place(x = 185, y = 85)
en4 = tkinter.Text(top, bd=1, font=('等线', 10), width=20, height=5)
en4.place(x = 185, y = 110)

lab5 = tkinter.Label(top, text = "IDF")
lab5.place(x = 345, y = 85)
en5 = tkinter.Text(top, bd=1, font=('等线', 10), width=20, height=5)
en5.place(x = 345, y = 110)

lab6 = tkinter.Label(top, text = "s1 TF.IDF")
lab6.place(x = 25, y = 185)
en6 = tkinter.Text(top, bd=1, font=('等线', 10), width=20, height=5)
en6.place(x = 25, y = 220)

lab7 = tkinter.Label(top, text = "s2 TF.IDF")
lab7.place(x = 185, y = 185)
en7 = tkinter.Text(top, bd=1, font=('等线', 10), width=20, height=5)
en7.place(x = 185, y = 220)

#按钮
b = tkinter.Button(top, text = "计算", width = 8,command=main)
b.place(x = 400, y = 15)

top.mainloop()
